/* 引入jsencrypt实现数据RSA加密 */
import JSEncrypt from "jsencrypt" // 处理长文本数据时报错  jsencrypt.js Message too long for RSA
/* 引入encryptlong实现数据RSA加密 */
import encrypt from "encryptlong" // encryptlong是基于jsencrypt扩展的长文本分段加解密功能。

 

export default {
	/* JSEncrypt加密 */
  rsaPublicData (data) {
    const publicKey = '公钥key'
    let jsencrypt = new JSEncrypt()
    jsencrypt.setPublicKey(publicKey)
    let result = jsencrypt.encrypt(JSON.stringify(data))
    return result
  },
	/* JSEncrypt解密 */
	rsaPrivateData (data) {
	  const privateKey  = `-----BEGIN PRIVATE KEY-----
    MIICXAIBAAKBgQClPx3T+g/UKShlImR8tZmc2WAueYxgBbfIIOSg/nk3MHBmYL2dlmyE+VrDSdzvENTQYHAqTkploZcKUA1KcxGD9HEPobLvyfgxW/eOU/qwFLDR3AKom27LAV9m4Hnrbw/8Kin/O4Z3bj7foKObHmYcS3gkfhp2cfQc4Dj4PmDuiQIDAQABAoGAFjBDcRNCzNo58lqL47L+zXDi2Vq43qvl4ZmJGZUR8bcvYI/c5iwspi5VYaOpZvB7ov9QEF4y3zKzdU8KedS5pScIcYcDTTw33yMC4HCBiOLGQwQwt4ai+JgjTlYlXlqBr6MXYpNIR6NKg17xphwYG1+YIRxXCzdbTY86W2LG7gECQQDVDYYSFzFxKR5SvjahK3Y3IrTRyk374nPjek3spUvvSkxkIHDxWyE+GIzFd09Tn0uCeIUt84mCRdvpnkdA/QSlAkEAxo6TWfDMUxMqdSafvtqW1Lx9jOd9UYMNt506p9qLBdJsm/Ev9SQJhFWbMSLXB1ZsfQcMkJzuYQ/bMBcf2fLJFQJAE6ygCOOjaTPG3RU7rj55ODY4RHdOJEiRMxyEPao2DO7rhwOH6oHj2/wgY8vUw4AeVPoUMl4fAKy61iQLqHC1EQJBALQMbYnNVTqv0nxQ/eJvDP7zvVWcRIM5t6KcrKexAQfSfDIcT0SX3RlnF0dDNIGu2Mm3Lqmj5HWXj0bYcUWNZtECQHH4tY5tuemoKjFbMko7ve0zF+0tcgacOraDUvrCm8BIf5g0cXJg5YmpvOQ1bG7TN40QIftzRCBEcJ+CavXBYBU=
    -----END PRIVATE KEY-----`
	  let jsencrypt = new JSEncrypt()
	  jsencrypt.setPrivateKey(privateKey)
	  let result = jsencrypt.encrypt(JSON.stringify(data))
	  return result
	},
	encrypt (data) {
    const PUBLIC_KEY  = `-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQClPx3T+g/UKShlImR8tZmc2WAueYxgBbfIIOSg/nk3MHBmYL2dlmyE+VrDSdzvENTQYHAqTkploZcKUA1KcxGD9HEPobLvyfgxW/eOU/qwFLDR3AKom27LAV9m4Hnrbw/8Kin/O4Z3bj7foKObHmYcS3gkfhp2cfQc4Dj4PmDuiQIDAQAB
    -----END PUBLIC KEY-----`
    let encryptor = new encrypt()
    encryptor.setPublicKey(PUBLIC_KEY)
		/* 加密前必须把数据转化成字符串否则解不出来,找了好久才发现/(ㄒoㄒ)/~~ */
    const result = encryptor.encryptLong(JSON.stringify(data))
    return result
  },
  // 解密 - PRIVATE_KEY - 验证
  // @param data String
  decrypt(data) {
    const PRIVATE_KEY  = `-----BEGIN PRIVATE KEY-----
    MIICXAIBAAKBgQClPx3T+g/UKShlImR8tZmc2WAueYxgBbfIIOSg/nk3MHBmYL2dlmyE+VrDSdzvENTQYHAqTkploZcKUA1KcxGD9HEPobLvyfgxW/eOU/qwFLDR3AKom27LAV9m4Hnrbw/8Kin/O4Z3bj7foKObHmYcS3gkfhp2cfQc4Dj4PmDuiQIDAQABAoGAFjBDcRNCzNo58lqL47L+zXDi2Vq43qvl4ZmJGZUR8bcvYI/c5iwspi5VYaOpZvB7ov9QEF4y3zKzdU8KedS5pScIcYcDTTw33yMC4HCBiOLGQwQwt4ai+JgjTlYlXlqBr6MXYpNIR6NKg17xphwYG1+YIRxXCzdbTY86W2LG7gECQQDVDYYSFzFxKR5SvjahK3Y3IrTRyk374nPjek3spUvvSkxkIHDxWyE+GIzFd09Tn0uCeIUt84mCRdvpnkdA/QSlAkEAxo6TWfDMUxMqdSafvtqW1Lx9jOd9UYMNt506p9qLBdJsm/Ev9SQJhFWbMSLXB1ZsfQcMkJzuYQ/bMBcf2fLJFQJAE6ygCOOjaTPG3RU7rj55ODY4RHdOJEiRMxyEPao2DO7rhwOH6oHj2/wgY8vUw4AeVPoUMl4fAKy61iQLqHC1EQJBALQMbYnNVTqv0nxQ/eJvDP7zvVWcRIM5t6KcrKexAQfSfDIcT0SX3RlnF0dDNIGu2Mm3Lqmj5HWXj0bYcUWNZtECQHH4tY5tuemoKjFbMko7ve0zF+0tcgacOraDUvrCm8BIf5g0cXJg5YmpvOQ1bG7TN40QIftzRCBEcJ+CavXBYBU=
    -----END PRIVATE KEY-----`
    let encryptor = new encrypt()
    encryptor.setPrivateKey(PRIVATE_KEY)
		let result = encryptor.decryptLong(JSON.stringify(data));
		return result
  }
} 